package com.clsfd.database;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Subscription {

	public int SubscriptionID=0;
	public Timestamp Activatedtime=null;
	public String Msisdn="";
	public String Servicetype="";
	public int Serviceitem=0;
	public String Status="";
	public java.sql.Date Expiredate=null;
	public int LastAlertID=0;
	public Timestamp LastAlertSentTime=null;
	public int StartedAlertID=0;
	public String Language="eng";
	public DatabaseConnection MysqlConn=null;
	public int Alertcount=0;
	
	public Subscription(DatabaseConnection mysqlConn,int subscriptionID, Timestamp activatedtime, String msisdn,
			String servicetype, int serviceitem, String status, int lastalertid,
			java.sql.Date expiredate, int startedAlertID, String language,Timestamp lastalertSentTime, int alertcount) {
		super();
		SubscriptionID = subscriptionID;
		Activatedtime = activatedtime;
		Msisdn = msisdn;
		Servicetype = servicetype;
		Serviceitem = serviceitem;
		Status = status;
		Expiredate = expiredate;
		LastAlertID = lastalertid;
		StartedAlertID = startedAlertID;
		MysqlConn=mysqlConn;
		this.Language=language;
		LastAlertSentTime=lastalertSentTime;
		Alertcount=alertcount;
	}
	
	public Timestamp getActivatedtime() {
		return Activatedtime;
	}

	public void setActivatedtime(String activatedtime) throws SQLException, ParseException {
		this.Activatedtime = new Timestamp(( new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(activatedtime)).getTime());
		MysqlConn.sqlUpdate("update `subscriptions` set `activatedtime`='"+activatedtime+"' where `subscriptionID`="+SubscriptionID);
	}

	public Timestamp getLastAlertSentTime() {
		return LastAlertSentTime;
	}

	public void setLastAlertID(int lastalertid) throws SQLException, ParseException {
		//this.LastAlertSentTime = new Timestamp(( new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(lastalertsenttime)).getTime());
		
		this.LastAlertSentTime = new Timestamp((new Date()).getTime());
		
		if (lastalertid>this.getLastAlertID()){
			LastAlertID = lastalertid;
			MysqlConn.sqlUpdate("update `subscriptions` set `lastalertsenttime`='"+( new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()))+"', `lastAlertID`="+lastalertid+", `alertcount`=`alertcount`+1 where `subscriptionID`="+SubscriptionID);			
		}
		else if (lastalertid<this.getStartedAlertID()){
			StartedAlertID = lastalertid;
			MysqlConn.sqlUpdate("update `subscriptions` set `lastalertsenttime`='"+( new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()))+"', `startAlertID`="+lastalertid+", `alertcount`=`alertcount`+1  where `subscriptionID`="+SubscriptionID);			
		}
		
		
	}

	
	public String getMsisdn() {
		return Msisdn;
	}
	public void setMsisdn(String msisdn) throws SQLException {
		Msisdn = msisdn;
		MysqlConn.sqlUpdate("update `subscriptions` set `msisdn`='"+msisdn+"' where `subscriptionID`="+SubscriptionID);
	}


	public int getServiceitem() {
		return Serviceitem;
	}


	public void setServiceitem(int serviceitem) throws SQLException {
		this.Serviceitem = serviceitem;		
		MysqlConn.sqlUpdate("update `subscriptions` set `serviceitem`="+serviceitem+" where `subscriptionID`="+SubscriptionID);
	}


	public String getServicetype() {
		return Servicetype;
	}


	public void setServicetype(String servicetype) throws SQLException {
		this.Servicetype = servicetype;
		MysqlConn.sqlUpdate("update `subscriptions` set `servicetype`='"+servicetype+"' where `subscriptionID`="+SubscriptionID);
		
	}

	public String getStatus() {
		return Status;
	}


	public void setStatus(String status) throws SQLException {
		Status = status;
		MysqlConn.sqlUpdate("update `valerts` set `status`='"+status+"' where `subscriptionID`="+SubscriptionID);
	}


	public Date getExpiretedate() {
		return Expiredate;
	}


	public void setExpiretedate(String expiretedate) throws SQLException, ParseException {
		
		this.Expiredate = new java.sql.Date(( new SimpleDateFormat("yyyy-MM-dd").parse(expiretedate)).getTime());
		MysqlConn.sqlUpdate("update `subscriptions` set `expiredate`='"+expiretedate+"' where `subscriptionID`="+SubscriptionID);
	}



	public int getLastAlertID() {
		return LastAlertID;
	}
	
	public int getStartedAlertID() {
		return StartedAlertID;
	}
	public void setStartedAlertID(int startedAlertID) throws SQLException {
		StartedAlertID = startedAlertID;
		MysqlConn.sqlUpdate("update `subscriptions` set `startedAlertID`="+startedAlertID+" where `subscriptionID`="+SubscriptionID);
	}

	public String getLanguage() {
		return Language;
	}

	public void setLanguage(String language) throws SQLException {
		Language = language;
		MysqlConn.sqlUpdate("update `language` set `language`='"+language+"' where `msisdn`='"+this.Msisdn+"'");
	}
	
	public void deleteSubscription() throws SQLException {
		MysqlConn.sqlUpdate("delete from `subscriptions` where `subscriptionID`="+SubscriptionID);
	}

	public int getAlertcount() {
		return Alertcount;
	}

	public void setAlertcount(int alertcount) {
		Alertcount = alertcount;
	}

	public int getSubscriptionID() {
		return SubscriptionID;
	}
	
}
